#ifndef MYSQLSERVER_H
#define MYSQLSERVER_H
//aa

#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDateTime>
#include <QVariant>
#include <string>
#include <QList>
#include <QDebug>
#include <QMap>
#include <QDateTime>
#include <iterator>
#include<QTime>
#include<QWidget>
#include<QPushButton>
#include<QDebug>
#include<QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QAxObject>
#include<QGridLayout>
#include<QString>
#include<QStringList>
#include <QDir>
#include<windows.h>
#include <QJsonArray>
#include <QJsonObject>


//存了乙烯入管 出管 COT 的48 根管的温度，时间结构体
struct ethlene_databases{


    //炉号

    int forenceNum[48];
    int forenceNum1[48];
    int forenceNum2[48];

    //各个管对应的时间
    //入管时间数组
    QDateTime time[48];
    //出管时间数组
    QDateTime time1[48];
    //COT管时间数组
    QDateTime time2[48];

    //各个管对应的温度
    //入管温度数组
    int tube_in_temps[48];
    //出管温度数组
    int tube_out_temps[48];
    //COT温度数组
    int cot_temp[48];
};

//简单数据结构，温度，时间
struct datas_time{
    int temp;
    QDateTime time;
};




class MysqlServer : public QObject
{
    Q_OBJECT
public:

    explicit MysqlServer(QObject *parent = 0);

    /**
      *@breif:MysqlServer::pushDatas
      * @description:对应炉号管好吧数据插入对应表当中
      * @param:tubeNum
      * @param:forunNum
      * @param:location
      * @param:tempnam
      * @param:dataTime
      * @author: tommego
      * @time: 2016-05-15
      **/
    Q_INVOKABLE void pushDatas(QString tubeNum,//炉号
                               QString forunNum,//管号
                               QString location,//位置（tube in ,tube out ,tube cot)
                               QString temp,//温度
                               QString dataTime//时间
                               );

    /**
     * @brief mdeal_with
     * @param mdt
     * @description:计算日平均温度处理算法
     * @author: tommego
      * @time: 2016-05-15
     */
    void mdeal_with(QList<datas_time> *mdt);

    /**
      * @breif:MysqlServer::get_aler_temps_value
      * @param:i
      * @description:从数据库获取警戒温度
      * @author: tommego
      * @time: 2016-05-15
      * */
    Q_INVOKABLE int get_aler_temps_value(int i){
        return this->aler_temps[i];
    }
    /**
      * @breif:MysqlServer::set_aler_temps_value
      * @param:tube_in_aler_datas
      * @param:tube_out_aler_temp
      * @author: tommego
      * @time: 2016-05-15
      *
      * */
    Q_INVOKABLE void set_aler_temps_value(QString tube_in_aler_temp,QString tube_out_aler_temp,QString tube_cot_aler_temp,QString tube_gap_aler_temp){
        //创建
        QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost");
        db.setDatabaseName("schema1");
        db.setUserName("root");
        db.setPassword("");
        qDebug()<<"in:"<<tube_in_aler_temp<<"out:"<<tube_out_aler_temp<<"cot:"<<tube_cot_aler_temp<<"gap:"<<tube_gap_aler_temp<<endl;

        //链接数据库
        if(db.open()){

            qDebug()<<"database is established!";
        }
        else{
            qDebug()<<"faled to connect to database";
        }

        //获取入管诊断数据
        QSqlQuery query;
        query.clear();
        qDebug()<<"ajdkfhsd查询检测"<<query.exec("UPDATE `schema1`.`aler_temps` SET `tube_in_temp`='"+tube_in_aler_temp+"', `tube_out_temp`='"+tube_out_aler_temp+"', `tube_cot_temp`='"+tube_cot_aler_temp+"', `tube_gap_temp`='"+tube_gap_aler_temp+"' WHERE `idaler_temps`='1'");
        db.close();
        init_aler_temps();
    }


    //已检查 2016.01.20
    /**
      * @breif: MysqlServer::compare_datas
      * @param: forunceNum
      * @param: tubeNum
      * @param: from_DateTime
      * @param: to_DateTime
      * @description: 管管比较
      * @author: tommego
      * @time: 2016-05-15
      * */
    Q_INVOKABLE QJsonArray compare_datas(int forunceNum,int tubeNum,QDateTime from_DateTime,QDateTime to_DateTime);

    /****************************测试与诊断查询***********************************/
    Q_INVOKABLE void testDatas(int forunceNum,int tubeNum,QDateTime from_DateTime,QDateTime to_DateTime);
    /****************************测试与诊断查询***********************************/

    /****************************全管查询***********************************/
    Q_INVOKABLE void all_tube_show(int forunceNum,QDateTime from_DateTime, QDateTime to_DateTime);
    /****************************全管查询***********************************/

    /**
      * @breif: MysqlServer::access_tube_in_temp (),MysqlServer::access_tube_out_temp (),MysqlServer::access_tube_cot_temp ()
      * @description: 获取最新的入管，出管，ＣＯＴ温度
      * @author: tommego
      * @time: 2016-05-15
      **/
    Q_INVOKABLE QJsonArray access_tube_in_temp();
    Q_INVOKABLE QJsonArray access_tube_out_temp();
    Q_INVOKABLE QJsonArray access_tube_cot_temp();

    /**
      * @breif: MysqlServer::refresh_data
      * @description: 更新最新显示数据
      * @author: tommego
      * @time: 2016-05-15
      **/
    Q_INVOKABLE void refresh_data(){
        this->access_tube_in_temp();
        this->access_tube_out_temp();
//        this->access_tube_cot_temp();
    }

    /*********************************把获取的温度暴露给qml**************************************/
    //把获取的温度暴露给qml

    Q_INVOKABLE int get_all_tube_in_temp(int tubeNum , int id){
        datas_time a = tube_in_full_search_datas[tubeNum-1].at(id);
        return a.temp;
    }
    Q_INVOKABLE int get_all_tube_out_temp(int tubeNum , int id){
        datas_time a = tube_out_full_search_datas[tubeNum-1].at(id);
        return a.temp;
    }
    Q_INVOKABLE int get_all_tube_cot_temp(int tubeNum , int id){
        datas_time a = tube_cot_full_search_datas[tubeNum-1].at(id);
        return a.temp;
    }
    /*********************************把获取的温度暴露给qml**************************************/


    /*********************************把获取的比较温度暴露给qml**************************************/

    Q_INVOKABLE int get_test_in_datas_length(){
        return tube_in_test_datas.length();
    }
    Q_INVOKABLE int get_test_out_datas_length(){
        return tube_out_test_datas.length();
    }
    Q_INVOKABLE int get_test_cot_datas_length(){
        return tube_cot_test_datas.length();
    }

    Q_INVOKABLE int get_test_in_temp(int i){
        return tube_in_test_datas.at(i).temp;
    }
    Q_INVOKABLE int get_test_out_temp(int i){
        return tube_out_test_datas.at(i).temp;

    }
    Q_INVOKABLE int get_test_cot_temp(int i){
        return tube_cot_test_datas.at(i).temp;
    }

    Q_INVOKABLE QString get_test_in_time(int i){

        return tube_in_test_datas.at(i).time.toString("yy/M/d");
    }
    Q_INVOKABLE QString get_test_out_time(int i){
        return tube_out_test_datas.at(i).time.toString("yy/M/d");

    }
    Q_INVOKABLE QString get_test_cot_time(int i){
        return tube_cot_test_datas.at(i).time.toString("yy/M/d");

    }

    /*********************************把获取的比较温度暴露给qml**************************************/

    /*********************************获取时间**************************************/
    //获取时间
    Q_INVOKABLE QString get_all_tube_in_time(int tubeNum , int id){
        datas_time a = tube_in_full_search_datas[tubeNum-1].at(id);
        //a.time.toString(Qt::ISODate) = 2014-01-08T11:15:50
        QString b = a.time.toString(Qt::ISODate);
        //1997-07-16T19:20:30
        //0开始的2位
        b.replace(0,2,"");
        //97-07-16T19:20:30
        b.replace(2,1,"/");
        //97/07-16T19:20:30
        b.replace(5,1,"/");
        //97/07/16T19:20:30
        b.replace(8,1," ");
        //97/07/16 19:20:30
        return b;
    }
    Q_INVOKABLE QString get_all_tube_out_time(int tubeNum , int id){
        datas_time a = tube_out_full_search_datas[tubeNum-1].at(id);
        QString b = a.time.toString(Qt::ISODate);
        b.replace(0,2,"");
        b.replace(2,1,"/");
        b.replace(5,1,"/");
        b.replace(8,1," ");
        return b;
    }
    Q_INVOKABLE QString get_all_tube_cot_time(int tubeNum , int id){
        datas_time a = tube_cot_full_search_datas[tubeNum-1].at(id);
        QString b = a.time.toString(Qt::ISODate);
        b.replace(0,2,"");
        b.replace(2,1,"/");
        b.replace(5,1,"/");
        b.replace(8,1," ");
        return b;
    }
    /*********************************获取时间**************************************/

    /********************************获取全部查询个数*******************************************/
    Q_INVOKABLE int get_all_tube_in_count(int tubeNum){

        qDebug()<<"tubeInList中的个数 : "<<tube_in_full_search_datas[tubeNum-1].count();
        return tube_in_full_search_datas[tubeNum-1].count();
    }
    Q_INVOKABLE int get_all_tube_out_count(int tubeNum){
        qDebug()<<"tubeOutList中的个数 : "<<tube_out_full_search_datas[tubeNum-1].count();
        return tube_out_full_search_datas[tubeNum-1].count();
    }
    Q_INVOKABLE int get_all_tube_cot_count(int tubeNum){
        qDebug()<<"tubeCotList中的个数 : "<<tube_out_full_search_datas[tubeNum-1].count();
        return tube_cot_full_search_datas[tubeNum-1].count();
    }
    /********************************获取全部查询个数*******************************************/

    //已检查 2016.01.20
    /***********************导出EXCEL***********************************/
    QPushButton *btn1;
    QPushButton *btn2;
    QPushButton *btn3;
    QGridLayout *layout;
    QAxObject *excel;
    QAxObject *workbooks;
    QAxObject *workbook;
    QAxObject *worksheets;
    QAxObject *worksheet;
    /***********************导出EXCEL***********************************/

    //emitter
signals:

public slots:
    //导出EXCEl
    void exportExcel1();      //手动导出excel
    void exportExcel2();      //新建excel
    void exportExcel3();      //自动写入excel

private:
    //当前48根管最新的温度数据保存
    ethlene_databases my_ethlene_datas;

    //新方案诊断与检测数据存放
    QList<datas_time> tube_in_test_datas;
    QList<datas_time> tube_out_test_datas;
    QList<datas_time> tube_cot_test_datas;

    //全管查询后存放的数据 每个管的时间-温度数据列表
    QList<datas_time> tube_in_full_search_datas[48];
    QList<datas_time> tube_out_full_search_datas[48];
    QList<datas_time> tube_cot_full_search_datas[48];


    //默认警戒温度设置
    int aler_temps[4];

    /**
     * @brief init_aler_temps
     * @description: 初始化警告温度
     * @author: tommego
     * @time: 2016-05-15
     */
    void init_aler_temps();


};
#endif // MYSQLSERVER_H
